home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / bview.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-25  |  10.4 KB  |  441 lines

  1. /**
  2.    GRAB Graph Layout and Browser System
  3.  
  4.    Copyright (c) 1989, Tera Computer Company
  5.  **/
  6.  
  7.   /**
  8.      Implementation of buttons area 
  9.  
  10.      The area contains four buttons with menus attached, and 3 buttons to
  11.      indicate the current mode.  Pressing a MenuButton causes a menu to
  12.      popup.  The user then selects one of the menu items, which causes a
  13.      routine to be executed.  Pressing one of the mode buttons causes that
  14.      button to become the current mode.  Only one mode is current at a
  15.      time.
  16.  
  17.      The options button contains many items which indicate which global
  18.      options are set.  Because the values of these options can change, it
  19.      must be rebuilt often.
  20.  
  21.      At certain times, pressing buttons is inhibited.  At other times,
  22.      pressing buttons changes the outside state.
  23.    **/
  24.  
  25. #include "istring.h"
  26. #include "bview.h"
  27. #include "routines.h"
  28. #include "gframe.h"
  29. #include "mymenu.h"
  30. #include <InterViews/painter.h>
  31. #include <InterViews/sensor.h>
  32. #include <InterViews/shape.h>
  33. #include <InterViews/border.h>
  34. #include <InterViews/glue.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37.  
  38.   /* strings for the options menu */
  39. static char* otext[numopts] = 
  40. {
  41.     " Print Node Label",
  42.     " Mark Dummy Nodes",
  43.     " Draw Arrows",
  44.     " Print Edge Label",
  45.     " Change In Edges",
  46.     " Change Out Edges",
  47.     " Rotate PostScript File",
  48.     " Show BC",
  49.     " Debug Mode",
  50.     " Straighten Edges",
  51.     " Ignore Hidden"
  52. };
  53.  
  54. void commandpress(TextItem*), optionspress(TextItem*);
  55. void editpress(TextItem*), filepress(TextItem*);
  56.  
  57. ButtonView::ButtonView () 
  58.   /* build the button area:  4 menu buttons and 3 flipbuttons (mode buttons) */
  59. {
  60.     MyMenu* m;
  61.     ButtonState* b;
  62.     int i;
  63.  
  64.     b = new ButtonState();
  65.     m = new MyMenu();
  66.     m->Insert(new TextItem("Redisplay Screen", 'a'));
  67.     m->Insert(new TextItem("Redraw Graph", 'b'));
  68.     m->Insert(new TextItem("Layout Graph", 'c'));
  69.     m->Insert(new TextItem("Print Screen (PostScript)", 'd'));
  70.     m->Insert(new TextItem("Show Levels", 'e'));
  71.     m->Insert(new TextItem("Help", 'f'));
  72.     m->Insert(new TextItem("Exit", 'g'));
  73.     m->Compose();
  74.     commandButton = new MenuButton("Commands", b, m, commandpress);
  75.  
  76.     b = new ButtonState();
  77.     m = new MyMenu();
  78.     m->Insert(new TextItem("Change Node Text", 'a'));
  79.     m->Insert(new TextItem("Change Edge Label", 'b'));
  80.     m->Insert(new TextItem("Size to Fit", 'c'));
  81.     m->Insert(new TextItem("Focus on Node", 'd'));
  82.     m->Insert(new TextItem("Focus on Named Node", 'e'));
  83.     m->Insert(new TextItem("Create Checkpoint", 'f'));
  84.     m->Insert(new TextItem("Previous Checkpoint", 'g'));
  85.     m->Insert(new TextItem("Next Checkpoint", 'h'));
  86.     m->Insert(new TextItem("Execute Commands in File", 'i'));
  87.     m->Compose();
  88.     editButton = new MenuButton("Edit", b, m, editpress);
  89.  
  90.     b = new ButtonState();
  91.     m = new MyMenu();
  92.  
  93.     for (i = 0; i < numopts; i++)
  94.     {
  95.         tti[i] = new TextItem(otext[i], 'a' + i);
  96.         m->Insert(tti[i]);
  97.     }
  98.  
  99.     m->Insert(new TextItem("Set Zoom Gradient", 'A'));
  100.     m->Insert(new TextItem("Set Pan Gradient", 'B'));
  101.     m->Insert(new TextItem("Show Layout Stats", 'C'));
  102.     m->Insert(new TextItem("Show Program Stats", 'D'));
  103.     m->Insert(new TextItem("Show Region Sizes", 'E'));
  104.     m->Insert(new TextItem("Dump Node List", 'F'));
  105.     m->Insert(new TextItem("Open Attributes Window", 'G'));
  106.     m->Compose();
  107.     optionsButton = new MenuButton("Options", b, m, optionspress);
  108.  
  109.     b = new ButtonState();
  110.     m = new MyMenu();
  111.     m->Insert(new TextItem("Change File Name", 'a'));
  112.     m->Insert(new TextItem("Read File", 'b'));
  113.     m->Insert(new TextItem("Write File", 'c'));
  114.     m->Insert(new TextItem("Write File (PostScript)", 'd'));
  115.     m->Compose();
  116.     fileButton = new MenuButton("File", b, m, filepress);
  117.  
  118.     bmbstate = new ButtonState();
  119.     cmbstate = new ButtonState();
  120.     embstate = new ButtonState();
  121.     // rrh changed next three calls from DoBrowseMode to &DoBrowseMode (etc)
  122.     // in order to eliminate error messages from g++.  DoBrowseMode is
  123.     // an extern C function, not a C++ function.  complained about
  124.     // being given a "void (*)()", when it wanted a "auto void (*)()"
  125.     // mysterious
  126.     browseMButton = new FlipButton("Browse", bmbstate, false, true, 
  127.                    &DoBrowseMode);
  128.     changeMButton = new FlipButton("Change", cmbstate, true, false, 
  129.                    &DoChangeMode);
  130.     editMButton = new FlipButton("Edit", embstate, true, false, &DoEditMode);
  131.     browseMButton->SetOpp(editMButton, changeMButton);
  132.     editMButton->SetOpp(browseMButton, changeMButton);
  133.     changeMButton->SetOpp(browseMButton, editMButton);
  134.  
  135.     Insert (new HGlue);
  136.     Insert (commandButton);
  137.     Insert (new HGlue);
  138.     Insert (editButton);
  139.     Insert (new HGlue);
  140.     Insert (optionsButton);
  141.     Insert (new HGlue);
  142.     Insert (fileButton);
  143.     Insert (new HGlue);
  144.     Insert (browseMButton);
  145.     Insert (new HGlue);
  146.     Insert (changeMButton);
  147.     Insert (new HGlue);
  148.     Insert (editMButton);
  149.     Insert (new HGlue);
  150. }
  151.  
  152. void ButtonView::SetFrame(GraphFrame *frame)
  153.   /**
  154.      the buttons must know about the top-level interactor so they
  155.      can set the cursor to its proper value
  156.    **/
  157. {
  158.     commandButton->SetFrame(frame);
  159.     editButton->SetFrame(frame);
  160.     optionsButton->SetFrame(frame);
  161.     fileButton->SetFrame(frame);
  162.     browseMButton->SetFrame(frame);
  163.     editMButton->SetFrame(frame);
  164. }
  165.  
  166.   /**
  167.      Build a new options menu, according to the 'char' array b (b
  168.      really should be a boolean array, but I can't get it passed correctly,
  169.      so char will have to do).  In any event, the array indicates which options 
  170.      are set and which are not.  Those that are set get an asterisk before
  171.      them.
  172.    **/
  173. static const char on = '*';
  174. static const char off = ' ';
  175.  
  176. void ButtonView::NewOptionsMenu(char b[numopts]) 
  177. {
  178.     int i;
  179.     MyMenu* m = new MyMenu();
  180.  
  181.     for (i = 0; i < numopts; i++)
  182.     {
  183.         char* buf = strdup(otext[i]);
  184.     buf[0] = b[i] ? on : off;
  185. //    delete tti[i];
  186.         tti[i] = new TextItem(buf, 'a' + i);
  187.         m->Insert(tti[i]);
  188.     }
  189.  
  190.     m->Insert(new TextItem("Set Zoom Gradient", 'A'));
  191.     m->Insert(new TextItem("Set Pan Gradient", 'B'));
  192.     m->Insert(new TextItem("Show Layout Stats", 'C'));
  193.     m->Insert(new TextItem("Show Program Stats", 'D'));
  194.     m->Insert(new TextItem("Show Region Sizes", 'E'));
  195.     m->Insert(new TextItem("Dump Node List", 'F'));
  196.     m->Insert(new TextItem("Set Displayed Attributes", 'G'));
  197.     m->Compose();
  198.     optionsButton->ChangeMenu(m);
  199. }
  200.  
  201. MenuButton::MenuButton (char* s, ButtonState* b, MyMenu* m, 
  202.     void (*f)(TextItem*)) : (s, b, -1) 
  203.   /**
  204.      I'm not sure the buttonstate is worth keeping 
  205.      pressf is the function to call when a menu item is selected
  206.    **/
  207. {
  208.     menu = m;
  209.     bstate = b;
  210.     pressf = f;
  211.     frame = nil;
  212. }
  213.  
  214. void MenuButton::SetFrame(GraphFrame* f)
  215.   /* store the value of the top-level interactor */
  216. {
  217.     frame = f;
  218. }
  219.  
  220. void MenuButton::ChangeMenu (MyMenu* m) 
  221.   /**
  222.      change the menu this button is associated with (useful for the 
  223.      options menu button)
  224.    **/
  225. {
  226.     delete menu;
  227.     menu = m;
  228. }
  229.  
  230. void MenuButton::Handle (Event& e) 
  231.   /* on a down event, popup a menu, and call pressf if an item was selected */
  232. {
  233.     TextItem* i;
  234.     
  235.     if (e.eventType == DownEvent)
  236.     {
  237.     frame->SetCurs(mainC);
  238.     ClearInModeFlag();   /* if we were in a special state, we aren't now */
  239.         menu->Popup(e, i);
  240.  
  241.         if (i != nil) 
  242.     {
  243.         this->pressf(i);
  244.     }
  245.     }
  246. }
  247.  
  248. FlipButton::FlipButton (char* s, ButtonState* b, int on, int off, void (*f)()) 
  249.     : (s, b, on, off)
  250.   /**
  251.      a button which is either on or off.  If it's on, the other flipbuttons
  252.      are off.  
  253.    **/
  254. {
  255.     pressf = f;
  256.     onval = on;
  257.     offval = off;
  258.     opp1 = nil;
  259.     opp2 = nil;
  260.     frame = nil;
  261. }
  262.  
  263. void FlipButton::SetFrame(GraphFrame* f)
  264. {
  265.     frame = f;
  266. }
  267.  
  268. void FlipButton::SetOpp(FlipButton* f1, FlipButton* f2)
  269.   /** 
  270.      keep track of who the other flipbuttons are so we can inform them
  271.      when we've been selected
  272.    **/
  273. {
  274.     opp1 = f1;
  275.     opp2 = f2;
  276. }
  277.  
  278. void FlipButton::Press()
  279.   /* if we were off, turn ourselves on and turn the others off */
  280. {
  281.     ClearInModeFlag();    /* if we were in a special state, we aren't now */
  282.  
  283.     if (!chosen) 
  284.     {
  285.     subject->SetValue(onval);
  286.         this->pressf();
  287.     opp1->unpress();
  288.     opp2->unpress();
  289.     }
  290. }
  291.  
  292. void FlipButton::unpress()
  293. {
  294.     subject->SetValue(offval);
  295. }
  296.  
  297. void commandpress(TextItem* i) 
  298.   /* commands to be executed when command menu items are selected */
  299. {
  300.     switch (i->tag) 
  301.     {
  302.     case 'a':
  303.         DoRedisplay();
  304.         break;
  305.     case 'b':
  306.         DoRedrawGraph();
  307.         break;
  308.     case 'c':
  309.         DoLayoutGraph();
  310.         break;
  311.     case 'd':
  312.         DoPSScreen();
  313.         break;
  314.     case 'e':
  315.         DoShowLevels();
  316.         break;
  317.     case 'f':
  318.         DoHelp();
  319.         break;
  320.     case 'g':
  321.         DoQuit();
  322.         break;
  323.     }
  324. }
  325.  
  326. void editpress(TextItem* i) 
  327.   /* commands to be executed when edit menu items are selected */
  328. {
  329.     switch (i->tag) 
  330.     {
  331.     case 'a':
  332.         DoChangeText();
  333.         break;
  334.     case 'b':
  335.         DoChangeEdgeLabel();
  336.         break;
  337.     case 'c':
  338.         DoSizeToFit();
  339.         break;
  340.     case 'd':
  341.         DoFocusNode();
  342.         break;
  343.     case 'e':
  344.         DoFocusNamedNode();
  345.         break;
  346.     case 'f':
  347.         DoSaveCkpt();
  348.         break;
  349.     case 'g':
  350.         DoPrevCkpt();
  351.         break;
  352.     case 'h':
  353.         DoNextCkpt();
  354.         break;
  355.     case 'i':
  356.         DoGetCommandsFromFile();
  357.         break;
  358.     }
  359. }
  360.  
  361. void optionspress(TextItem* i) 
  362.   /* commands to be executed when options menu items are selected */
  363. {
  364.     switch (i->tag) 
  365.     {
  366.     case 'a':
  367.         DoPrintLabel();
  368.         break;
  369.     case 'b':
  370.         DoMarkDummyNodes();
  371.         break;
  372.     case 'c':
  373.         DoDrawArrow();
  374.         break;
  375.     case 'd':
  376.         DoPrintEdgeLabel();
  377.         break;
  378.     case 'e':
  379.         DoChangeInEdges();
  380.         break;
  381.     case 'f':
  382.         DoChangeOutEdges();
  383.         break;
  384.     case 'g':
  385.         DoRotatePSFile();
  386.         break;
  387.     case 'h':
  388.         DoShowBC();
  389.         break;
  390.     case 'i':
  391.         DoDebugMode();
  392.         break;
  393.     case 'j':
  394.         DoStraighten();
  395.         break;
  396.     case 'k':
  397.         DoIgnoreHidden();
  398.         break;
  399.     case 'A':
  400.         DoSetZoomGrad();
  401.         break;
  402.     case 'B':
  403.         DoSetPanGrad();
  404.         break;
  405.     case 'C':
  406.         DoPrintLayoutStats();
  407.         break;
  408.     case 'D':
  409.         DoPrintUsage();
  410.         break;
  411.     case 'E':
  412.         DoShowSize();
  413.         break;
  414.     case 'F':
  415.         DoDumpNodeList();
  416.         break;
  417.     case 'G':
  418.         DoSetDisplayed();
  419.     }
  420. }
  421.  
  422. void filepress(TextItem* i) 
  423.   /* commands to be executed when file menu items are selected */
  424. {
  425.     switch (i->tag) 
  426.     {
  427.     case 'a':
  428.         DoChangeFileName();
  429.         break;
  430.     case 'b':
  431.         DoReadFile();
  432.         break;
  433.     case 'c':
  434.         DoWriteFile();
  435.         break;
  436.     case 'd':
  437.         DoCreatePSFile();
  438.         break;
  439.     }
  440. }
  441.